package com.zzyl.nursing.controller;

import java.util.List;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.ProductDetail;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author ruoyi
 * @date 2025-07-20
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 同步设备列表
     */
    @GetMapping("/syncProductList")
    @ApiOperation(value = "同步设备列表")
    public AjaxResult syncProductList(Device device)
    {
        deviceService.syncProductList(device);
        return success();
    }

    @GetMapping("/allProduct")
    @ApiOperation(value = "获取所有产品列表")
    public R<List<ProductVo>> allProduct(){
        return R.ok(deviceService.allProduct());
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto){
        deviceService.registerDevice(deviceDto);
        return success();
    }

    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    @GetMapping("/{iotId}")
    @ApiOperation("查询设备详情")
    public R<DeviceDetailVo> getInfo(@PathVariable String iotId){
        return R.ok(deviceService.selectDeviceById(iotId));
    }

    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报的数据")
    public AjaxResult queryServiceProperties(@PathVariable String iotId){
        return deviceService.queryServiceProperties(iotId);
    }

    @PutMapping
    @ApiOperation("修改设备")
    public AjaxResult edit(@RequestBody DeviceDto deviceDto){
        deviceService.edit(deviceDto);
        return success();
    }

    @DeleteMapping("/{iotId}")
    @ApiOperation("删除设备")
    public AjaxResult delete(@PathVariable String iotId){
        deviceService.deleteByIotId(iotId);
        return success();
    }

    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("查询产品详情")
    public R<List<ProductDetail>> getByProductKey(@PathVariable String productKey){
        return R.ok(deviceService.getByProductKey(productKey));
    }


}
